package com.example.pinkcat.activity.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.pinkcat.R;
import com.example.pinkcat.activity.Userpage;
import com.example.pinkcat.activity.VideoActivity;
import com.example.pinkcat.activity.VideoPinkcat;
import com.example.pinkcat.activity.api.entity.CommentEntity;
import com.example.pinkcat.activity.api.entity.VideoEntity;
import com.example.pinkcat.activity.api.entity.VideoInfoEntity;
import com.example.pinkcat.activity.api.getData.GetData;
import com.example.pinkcat.activity.changuserinfo.reportActivity;
import com.example.pinkcat.activity.pinkCatUser.userStaticMsg;
import com.rishabhharit.roundedimageview.RoundedImageView;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import retrofit2.http.GET;

public class V_recyleview_adapter extends RecyclerView.Adapter<V_recyleview_adapter.ViewHolder>{
    boolean ispage1;
    private final Context context;
    int pl_numbers;
    String up_id="-1";
    VideoInfoEntity videoInfoEntity;//第一个视频简介信息
    List<VideoEntity> videoEntities=new ArrayList<>();//推荐视频
    List<CommentEntity> commentEntities=new ArrayList<>();//评论信息

    //构造方法
    public V_recyleview_adapter(Context context, int pl_numbers, boolean ispage1,  String up_id){
        this.context=context;
        this.pl_numbers=pl_numbers;
        this.ispage1=ispage1;
        this.up_id=up_id;
    }
    //设置视频信息
    public void setvideoinfoData(VideoInfoEntity videoInfoEntity){
        if (videoInfoEntity!=null)
            this.videoInfoEntity=videoInfoEntity;
        notifyDataSetChanged();
    }
    //获取推荐视频
    public void setvideolistData(List<VideoEntity> videoEntities){
        if (videoEntities!=null){
            List<VideoEntity> videos=videoEntities;
            for (VideoEntity entity:videos){
                if (entity.getVideo_part_mp()==1){
                    this.videoEntities.add(entity);
                }
            }
        }
        notifyDataSetChanged();
    }

    //获取评论信息
    public void setcomentlistData(List<CommentEntity> commentEntities){
        if (commentEntities!=null)
            this.commentEntities=commentEntities;
        notifyDataSetChanged();
    }
    //内部内
    public static class ViewHolder extends RecyclerView.ViewHolder{
        //简介
        RoundedImageView userhead;//用户头像
        TextView username;//up名称
        TextView fansnum;//粉丝数量
        TextView video_title;//视频标题
        TextView video_palynum;//视频播放数量
        TextView video_pl_num;//视频评论数量
        TextView video_sendtime;//发送时间
        TextView video_idtxt;//视频id
        TextView video_introduction;//视频简介
        TextView addupbtn;//关注按钮
        ImageView likebtn;//点赞按钮
        TextView likebtn_num;//点赞数量
        ImageView unlikebtn;//点踩按钮
        TextView unlikebtn_num;//点踩数量
        ImageView followbtn;//收藏按钮
        TextView followbtn_num;//收藏数量
        ImageView sharebtn;//分享按钮
        View video_page_introduction;//整个页面

        //评论
        RoundedImageView pl_head;
        TextView pl_name;
        TextView pl_sendtime;//发送时间
        TextView pl_usersays;//视频描述
        TextView pl_likenum;//视频描述
        TextView pl_unlickenum;//视频描述

        ImageView pl_like;//点赞评论
        ImageView pl_unlike;//点踩评论
        ImageView pl_report;//举报评论

        //
        View aboutvideo;//推荐视频layoutview
        RoundedImageView headimg;//封面
        TextView v_title;//标题
        TextView v_username;//发布者名称
        TextView v_play;//播放量
        TextView v_pl;//评论量

        //寻找声明布局控件
        public ViewHolder(View view) {
            super(view);
            video_page_introduction=view.findViewById(R.id.video_page_introduction);
            userhead=view.findViewById(R.id.page1_userhead);
            username=view.findViewById(R.id.up_id);
            fansnum=view.findViewById(R.id.up_fans);
            video_title=view.findViewById(R.id.video_title);
            video_palynum=view.findViewById(R.id.bfln);
            video_pl_num=view.findViewById(R.id.plln);
            video_idtxt=view.findViewById(R.id.video_id);
            video_sendtime=view.findViewById(R.id.sned_data);
            video_introduction=view.findViewById(R.id.video_driction);
            likebtn=view.findViewById(R.id.v_like);
            likebtn_num=view.findViewById(R.id.v_like_num);
            unlikebtn=view.findViewById(R.id.v_unlike);
            unlikebtn_num=view.findViewById(R.id.v_unlike_num);
            followbtn=view.findViewById(R.id.v_follow);
            followbtn_num=view.findViewById(R.id.v_follow_num);
            sharebtn=view.findViewById(R.id.v_share);
            addupbtn=view.findViewById(R.id.followbtn);

            pl_sendtime=view.findViewById(R.id.send_time);
            pl_usersays=view.findViewById(R.id.text_pl);
            pl_head=view.findViewById(R.id.user_head);
            pl_name=view.findViewById(R.id.user_id);
            pl_likenum=view.findViewById(R.id.pl_like_num);//视频描述
            pl_unlickenum=view.findViewById(R.id.pl_unlike_num);//视频描述
            pl_like=view.findViewById(R.id.thum_img);
            pl_unlike=view.findViewById(R.id.unthum_img);
            pl_report=view.findViewById(R.id.repot);

            aboutvideo=view.findViewById(R.id.list_main_body);
            headimg=view.findViewById(R.id.video_face);
            v_title=view.findViewById(R.id.about_v_title);
            v_username=view.findViewById(R.id.about_username);
            v_play=view.findViewById(R.id.about_bfsl);
            v_pl=view.findViewById(R.id.about_plsl);
        }
    }
    //创建获取 view  布局
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //获取item内容
        View view;
        if (ispage1){//viewpager主页面
            if (viewType==0) {
                view= LayoutInflater.from(context).inflate(R.layout.list_videosimpleintroduce,parent,false);
            }
            else {
                view= LayoutInflater.from(context).inflate(R.layout.list_aboutvideo,parent,false);
            }
        }
        else {//viewpager评论页面
            view= LayoutInflater.from(context).inflate(R.layout.list_pinlunlist,parent,false);
        }
        return new ViewHolder(view);
    }

    //绑定数据
    @SuppressLint({"CheckResult", "SetTextI18n"})
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (ispage1){
            //页面1
            if (position==0){//视频简介
                if(videoInfoEntity!=null){//初始化简介
                    Glide.with(context).load(Uri.parse(videoInfoEntity.getHead_photo())).into(holder.userhead);
                    holder.username.setText(videoInfoEntity.getUser_name());
                    holder.fansnum.setText(videoInfoEntity.getFans_num()+"粉丝");
                    holder.video_title.setText(videoInfoEntity.getVideo_name());
                    holder.video_palynum.setText(videoInfoEntity.getTotalplay());
                    holder.video_pl_num.setText(videoInfoEntity.getComment_count());
                    String[] ss=videoInfoEntity.getVideo_pub_time().split(" ");
                    holder.video_sendtime.setText(ss[0]);//时间
                    holder.video_idtxt.setText("vd"+videoInfoEntity.getVideo_id());
                    holder.video_introduction.setText(videoInfoEntity.getVideo_info());
                    holder.likebtn_num.setText(videoInfoEntity.getLike_num());
                    holder.unlikebtn_num.setText(videoInfoEntity.getUnlike_num());
                    holder.followbtn_num.setText(videoInfoEntity.getCollect_num());
                    holder.sharebtn.setSelected(false);//点赞按钮
                    if (userStaticMsg.userId.equals("-1")){//未登陆状态
                        holder.addupbtn.setText(R.string.follow);//关注按钮
                        holder.addupbtn.setTextColor(context.getResources().getColor(R.color.white));
                        holder.addupbtn.setBackground(context.getResources().getDrawable(R.drawable.backgroud_cycle_pink));
                        holder.likebtn.setSelected(false);//点赞按钮
                        holder.unlikebtn.setSelected(false);//点赞按钮
                        holder.followbtn.setSelected(false);//点赞按钮
                    }
                    else {//登陆状态
                        if (videoInfoEntity.getKeep()==null){//未关注
                            holder.addupbtn.setText(R.string.follow);//关注按钮
                            holder.addupbtn.setTextColor(context.getResources().getColor(R.color.white));
                            holder.addupbtn.setBackground(context.getResources().getDrawable(R.drawable.backgroud_cycle_pink));
                        }else {//以关注
                            holder.addupbtn.setText(R.string.unfollow);//关注按钮
                            holder.addupbtn.setTextColor(context.getResources().getColor(R.color.black_overlay));
                            holder.addupbtn.setBackground(context.getResources().getDrawable(R.drawable.backgroud_cycle_grey));
                        }

                        if (videoInfoEntity.getLike()==null||videoInfoEntity.getLike().equals("0")){//未点赞未点踩
                            holder.likebtn.setSelected(false);//点赞按钮
                            holder.unlikebtn.setSelected(false);//点赞按钮
                        }else if (videoInfoEntity.getLike().equals("1")){
                            holder.likebtn.setSelected(true);//点赞按钮
                            holder.unlikebtn.setSelected(false);//点赞按钮
                        }
                        else {
                            holder.likebtn.setSelected(false);//点赞按钮
                            holder.unlikebtn.setSelected(true);//点赞按钮
                        }

                        if (videoInfoEntity.getCollect()==null){//未收藏
                            holder.followbtn.setSelected(false);//点赞按钮
                        }else {
                            holder.followbtn.setSelected(true);//点赞按钮
                        }
                    }

                }
                listen(holder);//监听事件
            }
            else {//相关视频
                //推荐视频list
                //为视频初始化数据
                if (videoEntities.get(position-1)!=null){
                    Glide.with(context).load(Uri.parse(videoEntities.get(position-1).getVideo_cover())).into(holder.headimg);
                    holder.v_title.setText(videoEntities.get(position-1).getVideo_name());
                    holder.v_username.setText(videoEntities.get(position-1).getUser_name());
                    holder.v_play.setText(String.valueOf(videoEntities.get(position-1).getTotalplay()));
                    holder.v_pl.setText(String.valueOf(videoEntities.get(position-1).getComment_count()));
                }

                //点击监听事件
                holder.aboutvideo.setOnClickListener(new View.OnClickListener() {//点击事件
                    @Override
                    public void onClick(View v) {
                        if (videoEntities.get(position-1).getVideo_type().equals("综艺")||
                                videoEntities.get(position-1).getVideo_type().equals("电视剧")||
                                videoEntities.get(position-1).getVideo_type().equals("电影")||
                                videoEntities.get(position-1).getVideo_type().equals("动漫"))
                        {
                            Intent intent=new Intent(context, VideoPinkcat.class);
                            intent.putExtra("v_id",videoEntities.get(position-1).getVideo_id());
                            intent.putExtra("up_id",videoEntities.get(position-1).getUser_id());
                            intent.putExtra("v_link",videoEntities.get(position-1).getVideo_link());
                            intent.putExtra("v_labs",videoEntities.get(position-1).getVideo_lab());
                        }
                        else {
                            Intent intent=new Intent(context, VideoActivity.class);
                            intent.putExtra("v_id",videoEntities.get(position-1).getVideo_id());
                            intent.putExtra("up_id",videoEntities.get(position-1).getUser_id());
                            intent.putExtra("v_link",videoEntities.get(position-1).getVideo_link());
                            intent.putExtra("v_labs",videoEntities.get(position-1).getVideo_lab());
                            context.startActivity(intent);
                        }
                    }
                });
            }
        }//页面一相关视频
        else {
            //页面二评论
            if (commentEntities.size()>0){
                Glide.with(context).load(Uri.parse(commentEntities.get(position).getHead_photo())).into(holder.pl_head);
                holder.pl_name.setText(commentEntities.get(position).getUser_name());
                SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd");
                holder.pl_sendtime.setText(format.format(commentEntities.get(position).getComment_time()));
                holder.pl_usersays.setText(commentEntities.get(position).getComment_message());
                holder.pl_likenum.setText(commentEntities.get(position).getLike_num());
                holder.pl_unlickenum.setText(commentEntities.get(position).getUnlike_num());
            }
            if (holder.pl_like!=null){
                holder.pl_like.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (userStaticMsg.userId.equals("-1")){
                            Toast.makeText(context,"请登录",Toast.LENGTH_SHORT).show();
                        }else if (holder.pl_like.isSelected()){
                            Toast.makeText(context,"已经赞过了",Toast.LENGTH_SHORT).show();
                        }
                        else {
                            GetData.thumbOrUnthumbpl(context,commentEntities.get(position).getComment_id(),true);

                            holder.pl_like.setSelected(true);
                            int a=Integer.parseInt(holder.pl_likenum.getText().toString());
                            holder.pl_likenum.setText(String.valueOf(a+1));
                            holder.pl_unlike.setSelected(false);
                        }
                    }
                });//收藏事件
            }
            if (holder.pl_unlike!=null){
                holder.pl_unlike.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (userStaticMsg.userId.equals("-1")){
                            Toast.makeText(context,"请登录",Toast.LENGTH_SHORT).show();
                        }else if (holder.pl_unlike.isSelected()){
                            Toast.makeText(context,"已经踩过了",Toast.LENGTH_SHORT).show();
                        }
                        else {
                            GetData.thumbOrUnthumbpl(context,commentEntities.get(position).getComment_id(),false);

                            holder.pl_like.setSelected(false);

                            holder.pl_unlike.setSelected(true);
                            int a=Integer.parseInt(holder.pl_unlickenum.getText().toString());
                            holder.pl_unlickenum.setText(String.valueOf(a+1));
                        }
                    }
                });//收藏事件
            }

            if (holder.pl_report!=null){
                holder.pl_report.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (userStaticMsg.userId.equals("-1")) {
                            Toast.makeText(context, "请登录", Toast.LENGTH_SHORT).show();
                        }else {
                            Intent intent=new Intent(context, reportActivity.class);
                            intent.putExtra("comment_id",commentEntities.get(position).getComment_id());
                            intent.putExtra("comment_message",commentEntities.get(position).getComment_message());
                            context.startActivity(intent);
                        }
                    }
                });//收藏事件
            }
        }

    }

    @Override
    public int getItemCount() {
        if (ispage1)
            return videoEntities.size()+1;
        else return commentEntities.size();
    }


    @Override
    public int getItemViewType(int position) {
        if (position==0){
            return 0;
        }else {
            return 1;
        }
    }

    void listen(ViewHolder holder){
        //点击头像跳转个人界面
        holder.userhead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, Userpage.class);
                intent.putExtra("up_id",up_id);
                context.startActivity(intent);
            }
        });
        //关注按钮
        holder.addupbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (userStaticMsg.userId.equals("-1")){
                    Toast.makeText(context,"请先登陆",Toast.LENGTH_SHORT).show();
                    return;
                }

                if (holder.addupbtn.getText().equals("+ 关 注")){
                    if (GetData.followOrUnfollow(context,up_id,true)){
                        holder.addupbtn.setText(R.string.unfollow);
                        holder.addupbtn.setTextColor(context.getResources().getColor(R.color.black_overlay));
                        holder.addupbtn.setBackground(context.getResources().getDrawable(R.drawable.backgroud_cycle_grey));
                    }

                }
                else {
                    if (GetData.followOrUnfollow(context,up_id,false)){
                        holder.addupbtn.setText(R.string.follow);
                        holder.addupbtn.setTextColor(context.getResources().getColor(R.color.white));
                        holder.addupbtn.setBackground(context.getResources().getDrawable(R.drawable.backgroud_cycle_pink));
                    }
                }
            }
        });//关注事件
        holder.likebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.likebtn.isSelected()){
                    Toast.makeText(context,"已经点赞过了",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(GetData.video_like_follow_un(context,videoInfoEntity.getVideo_id(),"like")){
                    holder.likebtn.setSelected(true);
                    holder.unlikebtn.setSelected(false);
                }
            }
        });//点赞事件
        holder.unlikebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.unlikebtn.isSelected()){
                    Toast.makeText(context,"已经踩过了",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(GetData.video_like_follow_un(context,videoInfoEntity.getVideo_id(),"unlike")){
                    holder.likebtn.setSelected(false);
                    holder.unlikebtn.setSelected(true);
                }
            }
        });//点踩事件
        holder.followbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.followbtn.isSelected()){
                    if(GetData.video_like_follow_un(context,videoInfoEntity.getVideo_id(),"unfollow")){
                        holder.followbtn.setSelected(false);
                    }
                }
                else {
                    if(GetData.video_like_follow_un(context,videoInfoEntity.getVideo_id(),"follow")){
                        holder.followbtn.setSelected(true);
                    }
                }
            }
        });//收藏事件
        holder.sharebtn.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("ShowToast")
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"感谢分享~",Toast.LENGTH_SHORT).show();
            }
        });//分享事件
    }
}
